package com.dongan.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dongan.mapper.MenuRoleMapper;
import com.dongan.pojo.Codes;
import com.dongan.mapper.CodesMapper;
import com.dongan.pojo.MenuRole;
import com.dongan.service.CodesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dongan.service.MenuRoleService;
import com.dongan.service.MenusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author longTao
 * @since 2021-06-15
 */
@Service
public class MenuRoleServiceImpl extends ServiceImpl<MenuRoleMapper, MenuRole> implements MenuRoleService {

    @Autowired
    private MenusService service;
    @Override
    public List<Long> queryMenuBasedOnRoleId(Long id) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("ROLE_ID",id);
        queryWrapper.select("MENU_ID");
        List<Long> list = this.list(queryWrapper);
        return list;
    }

    /**
     * 菜单分配
     *
     * @param id 角色id
     * @param arr 菜单数组(添加)
     */
    @Override
    public void menuDistribution(Long id, Long[] arr) {
        QueryWrapper<MenuRole> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("ROLE_ID",id);
        this.remove(queryWrapper);
        List<MenuRole> list=new ArrayList<>();
        for (Long aLong : arr) {
            MenuRole role=new MenuRole();
            role.setRoleId(id);
            role.setMenuId(aLong);
            list.add(role);
        }
        this.saveBatch(list);
    }
}
